﻿myApp.controller("Add_AddressCtrl", ['$scope', '$http', '$routeParams', '$restUrls', '$post', '$rootScope', function ($scope, $http, $routeParams, $restUrls, $post, $rootScope) {
    $scope.CountryList = [];
    $scope.ZoneList = [];
    $scope.DistrictList = [];
    $scope.Address = {
        firstname: '',
        telephone: '',
        format: '',
        country_id: 0,
        zone_id: 0,
        district_id: 0,
        address_1: ''
    };
    var choose_country = {};
    var choose_zone = {};
    var choose_district = {};
    //获取省级数据
    $scope.GetCountryList = function () {
        $http({ method: "GET", url: $restUrls["getCountry"], params: {} }).success(function (result) {
            if (result.success == 1) {
                $scope.CountryList = result.data;
            }
        }).error(function (data, status, headers, config) {
            $.scojs_message('服务器忙,请稍候重试', $.scojs_message.TYPE_ERROR);
        });
    }
    $scope.GetCountryList();
    //选择省
    $scope.ChooseCountry = function (country) {
        $scope.ProvinceShow = false;
        $scope.CityShow = true;
        $scope.CountryShow = false;

        choose_country = country;
        $http({ method: "GET", url: $restUrls["getZone"], params: { country_id: country.country_id } }).success(function (result) {
            if (result.success == 1) {
                $scope.ZoneList = result.data;
            }
        }).error(function (data, status, headers, config) {
            $.scojs_message('服务器忙,请稍候重试', $.scojs_message.TYPE_ERROR);
        });
    }
    //选择市
    $scope.ChooseZone = function (zone) {
        $scope.ProvinceShow = false;
        $scope.CityShow = false;
        $scope.CountryShow = true;
        choose_zone = zone;
        $http({ method: "GET", url: $restUrls["getDistrict"], params: { zone_id: zone.zone_id } }).success(function (result) {
            if (result.success == 1) {
                $scope.DistrictList = result.data;
            }
        }).error(function (data, status, headers, config) {
            $.scojs_message('服务器忙,请稍候重试', $.scojs_message.TYPE_ERROR);
        });
    }
    //选择区
    $scope.ChooseDistrict = function (district) {
        choose_district = district;
        $scope.ProvinceShow = false;
        $scope.CityShow = false;
        $scope.CountryShow = false;
        $scope.AddressShow = false;
        $scope.Address.country_id = choose_country.country_id;
        $scope.Address.zone_id = choose_zone.zone_id;
        $scope.Address.district_id = choose_district.district_id;
        $scope.Address.format = choose_country.name + choose_zone.name + choose_district.name;
        choose_country = {};
        choose_zone = {};
        choose_district = {};
    }
    //保存地址
    $scope.SaveAddress = function () {
        if ($scope.AddAddressForm.$valid) {
            var parameters = {
                country_id: $scope.Address.country_id,
                zone_id: $scope.Address.zone_id,
                district_id: $scope.Address.district_id,
                firstname: $scope.Address.firstname,
                telephone: $scope.Address.telephone,
                address_1: $scope.Address.address_1,
                isdefault: 'default'
            }
            $post($restUrls["addAddress"], parameters).success(function (result) {
                if (result.success == 1) {
                    window.history.back(-2);
                } else {
                    $.scojs_message(result.message, $.scojs_message.TYPE_ERROR);
                }
            }).error(function (data, status, headers, config) {
                $.scojs_message('服务器忙,请稍候重试', $.scojs_message.TYPE_ERROR);
            });
        }
        else {
            $scope.showOneError = true;
        }
    }

}]);

myApp.controller("AddressListCtrl", ['$scope', '$http', '$routeParams', '$restUrls', '$post', '$rootScope', function ($scope, $http, $routeParams, $restUrls, $post, $rootScope) {
    $scope.AddressList = [];
    //获取地址列表
    $scope.GetAddessList = function () {
        $http({ method: "GET", url: $restUrls["getAddessList"], params: {} }).success(function (result) {
            if (result.success == 1) {
                $scope.AddressList = result.data.addresses;
            }
        }).error(function (data, status, headers, config) {
            $.scojs_message('服务器忙,请稍候重试', $.scojs_message.TYPE_ERROR);
        });
    }
    $scope.GetAddessList();
    $scope.SetDefault = function (address) {
        $http({ method: "GET", url: $restUrls["setDefaultAddress"], params: { address_id: address.address_id } }).success(function (result) {
            if (result.success == 1) {
                window.history.back();
            }
        }).error(function (data, status, headers, config) {
            $.scojs_message('服务器忙,请稍候重试', $.scojs_message.TYPE_ERROR);
        });
    }

}]);